#ifndef EVENT_LOOP_H
#define EVENT_LOOP_H

#include "common.h"

#define MAX_EVENTS 1000
#define MAX_EVENT_TYPES 32

typedef void (*event_handler_t)(event_t*);

typedef struct event_queue_node {
    event_t event;
    struct event_queue_node *next;
} event_queue_node_t;

typedef struct {
    event_queue_node_t *head;
    event_queue_node_t *tail;
    size_t size;
    size_t max_size;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} event_queue_t;

typedef struct {
    event_queue_t queue;
    pthread_t thread;
    bool running;
    event_handler_t handlers[MAX_EVENT_TYPES];
    char *name;
} event_loop_t;

// 事件循环管理
int event_loop_init(event_loop_t *loop, const char *name, size_t max_size);
int event_loop_register_handler(event_loop_t *loop, event_type_t type, event_handler_t handler);
int event_loop_send_event(event_loop_t *loop, event_t *event);
int event_loop_start(event_loop_t *loop);
void event_loop_stop(event_loop_t *loop);
void event_loop_cleanup(event_loop_t *loop);

// 事件工具函数
event_t *event_create(event_type_t type, void *data, size_t data_len, void (*cleanup_fn)(void*));
void event_destroy(event_t *event);

#endif
